package com.msstudio;

// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 

import com.msstudio.utils.PkgTools;
import com.msstudio.utils.HttpPosterListener;
import com.msstudio.midlets.MSCMIDlet;
import java.util.*;
import javax.microedition.lcdui.*;

public class GetMsgDlg
    implements CommandListener, HttpPosterListener
{

    protected Command cmdBack;
    protected Command cmdReply;
    private Timer getMsgTimer;
    private Vector vComMsg;
    private Vector vSysMsg;
    private int iComMsgNum;
    private int iSysMsgNum;
    private int iTimerCnt;
    private int iTimerToGet;
    private String strFromUin;
    private long curFindMinUIN;
    private Displayable backMenu;
    private boolean bControl;
    public static final int C_UpdateTimer_Send = 0;
    private final int C_UpdateTimer_Receive = 1;
    private final int C_UpdateTimer_Stat = 2;
    private final int C_UpdateTimer_NoReceive = 3;
    private boolean timerOn;

    public GetMsgDlg()
    {
        cmdBack = null;
        cmdReply = null;
        getMsgTimer = null;
        vComMsg = null;
        vSysMsg = null;
        iComMsgNum = 0;
        iSysMsgNum = 0;
        iTimerCnt = 0;
        iTimerToGet = 1;
        strFromUin = null;
        curFindMinUIN = 0L;
        backMenu = null;
        bControl = true;
        timerOn = false;
    }

    public void init()
    {
        cmdBack = new Command(MSCMIDlet.multiLang.sBack, 2, 3);
        cmdReply = new Command(MSCMIDlet.multiLang.sAnswer, 1, 1);
    }

    public boolean getControl()
    {
        return bControl;
    }

    public void switchControl()
    {
        if(bControl)
        {
            bControl = false;
            iTimerToGet = 1;
            PkgTools.setOtherInfo(null, "0", null);
        } else
        {
            bControl = true;
            PkgTools.setOtherInfo(null, "1", null);
        }
    }

    public void updateTimerSet(int i)
    {
        if(!bControl)
            return;
        switch(i)
        {
        default:
            break;

        case 0: // '\0'
            if(iTimerToGet > 3)
                iTimerToGet = 3;
            break;

        case 1: // '\001'
            iTimerToGet = 1;
            break;

        case 2: // '\002'
            if(iTimerToGet > 3)
                iTimerToGet = 3;
            break;

        case 3: // '\003'
            if(iTimerToGet < 12)
                iTimerToGet++;
            break;
        }
    }

    public void show(boolean flag, long l)
    {
        if(!flag && readSysMsg())
        {
            return;
        } else
        {
            readComMsg(l);
            return;
        }
    }

    public int getComMsgNum()
    {
        return iComMsgNum;
    }

    public int getSysMsgNum()
    {
        return iSysMsgNum;
    }

    private void doRecvComMsg(String s, String s1)
    {
        try
        {
            Calendar calendar = Calendar.getInstance();
            StringBuffer stringbuffer = new StringBuffer();
            stringbuffer.append(calendar.get(2) + 1).append("/").append(calendar.get(5)).append(" ").append(calendar.get(11)).append(":").append(PkgTools.To2Str(calendar.get(12))).append(":").append(PkgTools.To2Str(calendar.get(13))).append("\n").append(MSCMIDlet.friendRecord.getNamebyID(Long.parseLong(s))).append("[").append(s).append("]:").append(s1);
            appendComMsg(s, stringbuffer.toString());
        }
        catch(Exception exception) { }
    }

    private void appendComMsg(String s, String s1)
    {
        int i = MSCMIDlet.friendRecord.getUserLocation(s);
        if(MSCMIDlet.friendRecord.getUserLocation(s) == -1)
        {
            MSCMIDlet.friendRecord.addStrangerRecord(s);
            MSCMIDlet.friendList.buildFriendList();
        } else
        if(MSCMIDlet.friendRecord.getStatebyIndex(i) != 30)
            MSCMIDlet.mainForm.updateStat(s, "10");
        MSCMIDlet.instance.playAlert();
        if(vComMsg == null)
            vComMsg = new Vector();
        vComMsg.addElement(s);
        vComMsg.addElement(s1);
        iComMsgNum++;
    }

    private void appendSysMsg(String s, String s1, String s2)
    {
        MSCMIDlet.instance.playAlert();
        if(vSysMsg == null)
            vSysMsg = new Vector();
        vSysMsg.addElement(s);
        vSysMsg.addElement(s1);
        vSysMsg.addElement(s2);
        iSysMsgNum++;
    }

    public int findMsgByUIN(long l)
    {
        int i = -1;
        if(vComMsg == null)
            return -1;
        if(vComMsg.size() == 0)
            return -1;
        for(int j = 0; j < vComMsg.size(); j += 2)
        {
            if(!((String)vComMsg.elementAt(j)).equals(Long.toString(l)))
                continue;
            i = j;
            break;
        }

        return i;
    }

    public long getNextNeedRead()
    {
        long l2 = 0x7fffffffL;
        if(vComMsg == null)
            return 0L;
        if(vComMsg.size() == 0)
            return 0L;
        try
        {
            for(int i = 0; i < vComMsg.size(); i += 2)
            {
                long l = Long.parseLong((String)vComMsg.elementAt(i));
                if(!MSCMIDlet.blackList.inBlackList(l) && l > curFindMinUIN && l < l2)
                    l2 = l;
            }

            if(l2 == 0x7fffffffL)
            {
                for(int j = 0; j < vComMsg.size(); j += 2)
                {
                    long l1 = Long.parseLong((String)vComMsg.elementAt(j));
                    if(!MSCMIDlet.blackList.inBlackList(l1) && l1 < l2)
                        l2 = l1;
                }

            }
            if(l2 == 0x7fffffffL)
                l2 = 0L;
            curFindMinUIN = l2;
        }
        catch(Exception exception)
        {
            l2 = 0L;
        }
        return l2;
    }

    private boolean fetchComMsg(StringBuffer stringbuffer, long l)
    {
        if(vComMsg == null)
            return false;
        int i;
        if(l == 0L)
        {
            if(vComMsg.size() == 0)
                return false;
            i = 0;
        } else
        {
            i = findMsgByUIN(l);
            if(i < 0)
                return false;
        }
        strFromUin = (String)vComMsg.elementAt(i);
        vComMsg.removeElementAt(i);
        String s = (String)vComMsg.elementAt(i);
        vComMsg.removeElementAt(i);
        MSCMIDlet.historyMsg.appendComRecMsg(strFromUin, s);
        stringbuffer.append(s);
        iComMsgNum--;
        if(iComMsgNum < 0)
            iComMsgNum = 0;
        return true;
    }

    private boolean readSysMsg()
    {
        if(vSysMsg == null)
            return false;
        if(vSysMsg.size() == 0)
            return false;
        int i = 0;
        try
        {
            i = Integer.parseInt((String)vSysMsg.elementAt(0));
        }
        catch(Exception exception)
        {
            return false;
        }
        vSysMsg.removeElementAt(0);
        String s = (String)vSysMsg.elementAt(0);
        vSysMsg.removeElementAt(0);
        String s1 = (String)vSysMsg.elementAt(0);
        vSysMsg.removeElementAt(0);
        MSCMIDlet.ackSvrMsg.show(i, s, s1);
        iSysMsgNum--;
        if(iSysMsgNum < 0)
            iSysMsgNum = 0;
        return true;
    }

    private void readComMsg(long l)
    {
        StringBuffer stringbuffer = new StringBuffer();
        boolean flag = fetchComMsg(stringbuffer, l);
        if(flag)
        {
            String s = new String(stringbuffer);
            showMessage(MSCMIDlet.multiLang.sNewMessage, s);
        }
    }

    protected void showMessage(String s, String s1)
    {
        Form form = new Form(s);
        form.append(s1);
        form.addCommand(cmdBack);
        form.addCommand(cmdReply);
        form.setCommandListener(this);
        MSCMIDlet.display.setCurrent(form);
    }

    public void commandAction(Command command, Displayable displayable)
    {
        try
        {
            if(command == cmdBack)
                MSCMIDlet.mainForm.show();
            else
            if(command == cmdReply)
                MSCMIDlet.sendMsgDlg.show(Long.parseLong(strFromUin), false);
        }
        catch(Exception exception) { }
    }

    public void onMessage(String s, String s1, String s2)
    {
        int i;
        try
        {
            if(MSCMIDlet.blackList.inBlackList(Long.parseLong(s1)))
                return;
        }
        catch(Exception exception)
        {
            return;
        }
        i = Integer.parseInt(s);
        switch(i)
        {
        default:
            break;

        case 3: // '\003'
            if(!s1.equals(""))
            {
                MSCMIDlet.friendRecord.deleteRecord(s1);
                MSCMIDlet.friendRecord.addRecord(s1);
                MSCMIDlet.mainForm.updateStat(s1, "10");
                MSCMIDlet.friendList.buildFriendList();
                MSCMIDlet.finger.doGetInfor(s1, null);
            }
            // fall through

        case 1: // '\001'
        case 2: // '\002'
        case 4: // '\004'
            if(!s1.equals(""))
            {
                appendSysMsg(s, s1, s2);
                updateTimerSet(1);
                return;
            }
            break;

        case 9: // '\t'
            if(!s1.equals("") && !s2.equals(""))
            {
                doRecvComMsg(s1, s2);
                updateTimerSet(1);
                return;
            }
            break;

        case 99: // 'c'
            if(!s1.equals(""))
            {
                MSCMIDlet.mainForm.updateStat(s1, s2);
                updateTimerSet(2);
                return;
            }
            break;
        }
        return;
    }

    public void receiveHttpResponse(String s)
    {
        String s1;
        String s2;
        s1 = PkgTools.GetParaVal("CMD", s).toUpperCase();
        s2 = PkgTools.GetParaVal("RES", s);
        if(s2.equals("20"))
        {
            MSCMIDlet.instance.onNetError(true);
            return;
        }
        int i;
        if(!s1.equals("GETMSGEX") || !s2.equals("0")) {
            return;
        }
        i = Integer.parseInt(PkgTools.GetParaVal("MN", s));
        if(i == 0)
        {
            updateTimerSet(3);
            return;
        }
        String s4;
        String s5;
        String as[];
        String as1[];
        String as2[];
        String s3 = PkgTools.GetParaVal("MT", s);
        s4 = PkgTools.GetParaVal("UN", s);
        s5 = PkgTools.GetParaVal("MG", s);
        as = new String[i];
        as1 = new String[i];
        as2 = new String[i];
        int j = 0;
        j = PkgTools.GetMultiPara(as, s3);
        if(i != j)
            return;
        int k = PkgTools.GetMultiPara(as1, s4);
        if(i != k)
            return;
        int l = PkgTools.GetMultiPara(as2, s5);
        if(i != l)
            return;
        try
        {
            for(int i1 = 0; i1 < i; i1++)
                onMessage(as[i1], as1[i1], PkgTools.Decodecgi(as2[i1]));

        }
        catch(Exception exception) { }
    }

    public void handleHttpError(String s)
    {
    }

    public void startTimer()
    {
        timerOn = true;
    }

    public void stopTimer()
    {
        timerOn = false;
    }

    public void OnTimer()
    {
        if(!timerOn)
            return;
        if(++iTimerCnt >= iTimerToGet)
        {
            MSCMIDlet.httpPoster.getMsg(this);
            iTimerCnt = 0;
        }
    }
}
